package com.aac.framwork.aacframwork.webservice;

import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.MediatorLiveData;
import android.arch.lifecycle.Observer;
import android.support.annotation.MainThread;
import android.support.annotation.WorkerThread;

import io.reactivex.annotations.NonNull;
import io.reactivex.annotations.Nullable;

public abstract class NetworkBoundResource<ResultType, RequestType> {

    private final MediatorLiveData<Resource<ResultType>> result = new MediatorLiveData<>();

    public NetworkBoundResource() {
        result.setValue((Resource<ResultType>) Resource.loading(null));
        final LiveData<ResultType> dbSource = loadFromDB();
        result.addSource(dbSource, data -> {
            result.removeSource(dbSource);
            if ( shouldFetch(data);){
                fetc
            }
        });
    }

    //存储网络请求返回的数据
    @WorkerThread
    protected abstract void saveCallResult(@NonNull RequestType item);

    //数据库检索结果决定是否网络请求
    @MainThread
    protected abstract void shouldFetch(@Nullable ResultType data);

    //获取书库数据
    @NonNull
    @MainThread
    protected abstract LiveData<ResultType> loadFromDB();


    //创建API
    @NonNull
    @MainThread
    protected abstract LiveData<ApiResponse<RequestType>> createCall();


    @MainThread
    protected void onFetchFailed() {

    }

    //获取liveData

    public final LiveData<Resource<ResultType>> getAsLiveData() {
        return null;
    }

    private class ApiResponse<RequestType> {
    }

    private void fetchFromNetwork(final LiveData<ResultType> dbSource) {

    }
}
